//
//  EFAMoreMdyPwdVC.m
//  efairy
//
//  Created by XiangDongMBP on 2018/1/9.
//  Copyright © 2018年 East. All rights reserved.
//

#import "EFAMoreMdyPwdVC.h"
#import "MBProgressHUD+XDPrompt.h"
#import "ApiManager.h"
#import "EFAModel.h"

@interface EFAMoreMdyPwdVC ()
@property (weak, nonatomic) IBOutlet UITextField *pwdOldTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdNew1TextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdNew2TextField;
@property (weak, nonatomic) IBOutlet UIButton *modifyBtn;
@end

@implementation EFAMoreMdyPwdVC

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (void)initNavigationBar {
    self.navigationItem.title = @"修改密码";
}

- (void)initSubViews {
    self.modifyBtn.layer.masksToBounds = YES;
    self.modifyBtn.layer.cornerRadius = 4;
    [self.modifyBtn addTarget:self action:@selector(modifyBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - delegate
#pragma mark - event response
- (void)modifyBtnTapped:(UIButton *)btn {
    if (![self checkUserPwd]) {
        return;
    }
    
    [self requestModifyPassword];
}

#pragma mark - private methods
- (BOOL)checkUserPwd {
    if (!self.pwdOldTextField.text.length) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请输入原始密码"];
        return NO;
    }
    if (!self.pwdNew1TextField.text.length) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请输入新密码"];
        return NO;
    }
    if (!self.pwdNew2TextField.text.length) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请再次输入新密码"];
        return NO;
    }
    if (![self.pwdNew2TextField.text isEqualToString:self.pwdNew1TextField.text]) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"两次输入的密码不一样"];
        return NO;
    }
    return YES;
}

- (void)exitViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - network request
- (void)requestModifyPassword {
    MBProgressHUD *hud = [MBProgressHUD xd_showHUDAddedTo:self.view animated:YES];
    
    NSDictionary *pars = @{@"efairyuser_id" : @([EFAModel sharedInstance].loginInfo.user_info.efairyuser_id),
                           @"access_token" : [EFAModel sharedInstance].loginInfo.access_token,
                           @"efairyuser_password_old" : self.pwdOldTextField.text,
                           @"efairyuser_password_new" : self.pwdNew1TextField.text};
    
    [[XDAFNetworking sharedInstance] PUT:[ApiManager apiPassword] parameters:pars success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"修改密码返回成功：%@", responseObject);
        [hud hideAnimated:YES];
        MBProgressHUD *temp = [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"修改成功"];
        temp.completionBlock = ^{
            [self exitViewController];
        };
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"修改密码失败：%@", error);
        [hud hideAnimated:NO];
        NSString *msg = [[ApiErrorCode sharedInstance] errorCodeMsgWithError:error];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:msg];
    }];
}

#pragma mark - getters and setters

@end
